﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MagicDot : OBJBase
{
    GameObject dotObj;
    public GameObject DotObj { get => dotObj; set => dotObj = value; }
    float attackCDTime = 3;
    int number;
    public MagicDot()
    {

    }
    public MagicDot(Vector3 vec, int number)
    {
        MonoMgr.GetInstance().AddUpdateListener(DetectionEnemy);
        DotObj = GameObject.Instantiate(Resources.Load<GameObject>("ObjectRes/Dot"));
        DotObj.transform.position = vec;
        DotObj.transform.LookAt(Camera.main.gameObject.transform.position);
        this.number = number;
    }
    void DetectionEnemy()
    {
        if (GameManager.GetInstance().Game_State == E_Game_State.Play)
        {
            attackCDTime = attackCDTime - 1f * Time.deltaTime;
            if (attackCDTime < 0)
            {
                for (int i = 0; i < EnemyController.GetInstance().enemyList.Count; i++)
                {
                    if (Vector3.Distance(EnemyController.GetInstance().enemyList[i].Obj.transform.position, DotObj.transform.position) < 10)
                    {
                        PropController.GetInstance().CreatDotProp(DotObj.transform.position, EnemyController.GetInstance().enemyList[i].Obj.transform.position);
                    }
                    if (Vector3.Distance(EnemyController.GetInstance().enemyList[i].Obj.transform.position, DotObj.transform.position) < 0.5f)
                    {
                        MagicController.GetInstance().MagicGraphs[number].Recycle();
                    }
                }
                attackCDTime = 3;
            }
        }
    }
    public void Recycle()
    {
        MonoMgr.GetInstance().RemoveUpdateListener(DetectionEnemy);
        GameObject.Destroy(DotObj);
    }
}
